package com.bsoft.template.web.assets;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsoft.template.common.Result;
import com.bsoft.template.entity.assets.AssetsGl;
import com.bsoft.template.service.assets.AssetsGlService;
import com.bsoft.template.util.RequestParamPaser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("assetsGl")
public class AssetsGlController {

    @Autowired
    private AssetsGlService assetsGlService;

    @GetMapping("/getList")
    public Result getList(HttpServletRequest request) {
        Map<String, String> params = RequestParamPaser.getParameters(request);
        IPage<AssetsGl> page = new Page<>(
                Long.parseLong(params.getOrDefault("page", "1")),
                Long.parseLong(params.getOrDefault("pageSize", "10"))
        );
        QueryWrapper<AssetsGl> wrapper = new QueryWrapper<>();
        wrapper.notIn("zt", "2", "3");
        if (StringUtils.isNotBlank(params.get("zclb"))) {
            wrapper.eq("zclb", params.get("zclb"));
        }
        if (StringUtils.isNotBlank(params.get("mc"))) {
            wrapper.eq("mc", params.get("mc"));
        }
        wrapper.orderByAsc("zclb","mc","pp");
        return assetsGlService.getListByPage(page, wrapper);
    }

    @GetMapping("/getAllList")
    public Result getAllList(HttpServletRequest request) {
        Map<String, String> params = RequestParamPaser.getParameters(request);
        QueryWrapper<AssetsGl> wrapper = new QueryWrapper<>();
        wrapper.notIn("zt", "2");
        return assetsGlService.list(wrapper);
    }

    @PostMapping("/save")
    public Result save(@RequestBody AssetsGl assetsGl) {
        if (assetsGl.getId() != null) {
            AssetsGl gl = assetsGlService.getAssetsGlById(assetsGl.getId());
//            if ("3".equals(assetsGl.getZt()) && "3".equals(gl.getZt())) {
//                return new Result().fail().message("物品已被领用");
//            }
            if ("3".equals(assetsGl.getZt()) && "2".equals(gl.getZt())) {
                return new Result().fail().message("物品已报废");
            }
        }
        return assetsGlService.saveOrUpdate(assetsGl);
    }

    @GetMapping("/remove")
    public Result remove(int id) {
        return assetsGlService.removeById(id);
    }
}
